package com.javasee.aiqueue;

import com.javasee.data.model.User;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.SerializationUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Project Name:
 * File Name   :
 * Package Name:
 *
 * @author : xinyanfei
 * @date :
 * Copyright (c) , 京东商城  All Rights Reserved.
 */

@RestController
public class IndexController {

    @Autowired
    private AiDataFileService aiDataFileService;

    @Autowired
    private AiIndexFileService aiIndexFileService;

    @GetMapping("/add")
    public ResponseEntity<FileQueueSoa.StoreResponse> addOne(){
        try {
            User user = ApplicationContextHolder.getBean(User.class);
            ResponseEntity<FileQueueSoa.StoreResponse> result =  store(Lists.newArrayList(user));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public ResponseEntity<FileQueueSoa.StoreResponse> store(Object object) throws IOException {
        return this.store(SerializationUtils.serialize(object));
    }

    public ResponseEntity<FileQueueSoa.StoreResponse> store(byte[] data) throws IOException{
        AiDataFileService.WriteResult result = aiDataFileService.writeData(data);
        return ResponseEntity.ok(new FileQueueSoa.StoreResponse(
                result.getWriteCount(),
                result.getOffset(),
                result.getLength(),
                "File stored successfully"
        ));
    }

}
